var UserData = require('UserData')
var COMMON_VALUE = require('Common')
cc.Class({
    extends: cc.Component,

    properties: {
        backgroundNode:{
            default:null,
            type:cc.Node,
        },
        restaurantListNode:{
            default:null,
            type:cc.Node
        },
        restaurantPrefab:{
            default:null,
            type:cc.Prefab
        },
        nightMask:{
            default:null,
            type:cc.Node
        },
        earthShakeButton:{
            default:null,
            type:cc.Node
        },
        daySky:{
            default:null,
            type:cc.Node
        },
        nightSky1:{
            default:null,
            type:cc.Node
        },
        nightSky2:{
            default:null,
            type:cc.Node
        },
        offlinePromptNode: cc.Node
    },
    checkTime(){
        var date = new Date()
        let h = date.getHours()
        if(h>16 || h< 8){
            this.UserData.runtimeState.day = false
        }else{
            this.UserData.runtimeState.day = true
        }
        if(this.UserData.runtimeState.day){
            this.lightOff()
        }else{
            this.lightOn()
        }
        this.checkOpenTime(date)
    },
    checkOpenTime(date){
        let basicTimeLimit = COMMON_VALUE.WORK_TIME        
        let h = date.getHours()
        
        let workTimeList = [[0,24]]
        
        for(let i=0;i<this.restaurantListNode.children.length;i++){
            let resComponent = this.restaurantListNode.children[i].getComponent('Restaurant')
            if(resComponent.resColor.toUpperCase() ==="YELLOW"){
                workTimeList = basicTimeLimit.YELLOW
            }
            if(resComponent.resColor.toUpperCase() ==="BLUE"){
                workTimeList = basicTimeLimit.BLUE
            }
            if(resComponent.resColor.toUpperCase() ==="RED"){
                workTimeList = basicTimeLimit.RED
            }
            let open =  false
            let labelContent = ""
            for(let i=0;i<workTimeList.length;i++){
                let duration = workTimeList[i]
                if(workTimeList.length ==1){
                    labelContent = workTimeList[0][0]+":00-"+workTimeList[0][1]+":00"
                }else{
                    labelContent = workTimeList[0][0]+":00-"+workTimeList[1][1]+":00"
                }
                labelContent = "黄金时段" + labelContent + "收益x2"

                if(h >= duration[0] && h < duration[1]){
                    open =true
                    break
                }
            }
            if(open || UserData.runtimeState.OverTime){
                resComponent.Open = true                
            }else{
                resComponent.Open = false                
            }
                resComponent.checkOpenStatus(labelContent)
                    resComponent.updateBuffMoney() 
                try{
                    resComponent.updateDisplay()
                }catch(err){
                    //cc.log(err)
                }
        }

    },
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {
    // },

    start () {
        UserData.CCdirector = cc.director

        try{
            wx.onShow(()=>{
                UserData.runtimeState.tempLeaveTime = (Date.now() - UserData.runtimeState.tempLeaveMoment)/1000
                UserData.runtimeState.HideScreen = false
            })
            wx.onHide(()=>{
                UserData.runtimeState.tempLeaveMoment = Date.now()
                UserData.runtimeState.HideScreen = true
                UserData.setCurrentMoney()
            })
        }catch(err){
            //cc.log(err)
        }
        
        this.UserData = UserData
        for(var i=0;i<10;i++){
            this.restaurantListNode.insertChild(cc.instantiate(this.restaurantPrefab))
            this.restaurantListNode.children[i].getComponent('Restaurant').floor = i+1
        }
        
        for(var j=0;j<this.restaurantListNode.children.length;j++){
            this.restaurantListNode.children[j].getComponent("Restaurant").loadUserData(this.UserData.state.restaurantList[j])
        }
        this.UserData = UserData    

        this.viewSize=cc.view.getVisibleSize()
        COMMON_VALUE.resolution = cc.v2(this.viewSize.width,this.viewSize.height)
        this.backgroundNode.width = this.viewSize.width
        this.checkTime()
        this.node.runAction(cc.repeatForever(cc.sequence(cc.delayTime(3),cc.callFunc(this.checkTime,this))))
        
    },

    lightOn(){
        this.nightMask.getChildByName('light1-AnimationCancel').opacity = 0
        this.nightMask.active = true
        this.daySky.active = false
        this.nightSky1.active = true
        this.nightSky2.active = true
        setTimeout(() => {
            this.nightMask.getChildByName('light1-AnimationCancel').runAction(cc.repeatForever(cc.sequence(
                cc.fadeTo(1.4,255).easing(cc.easeSineIn()),
                cc.fadeTo(1.4,130).easing(cc.easeSineOut())
            )))    
        }, 4000);



        this.nightMask.getChildByName('夜晚霓虹灯-AnimationCancel').runAction(cc.repeatForever(cc.sequence(
            cc.fadeTo(0.6,255),
            cc.fadeTo(0.6,80)
        )))    
 
 
        for(var i=0;i<this.restaurantListNode.children.length;i++){
            this.restaurantListNode.children[i].getComponent('ResSetup').lightOn()
        }
    },
    lightOff(){
        this.nightMask.active = false
        this.daySky.active = true
        this.nightSky1.active = false
        this.nightSky2.active = false
        for(var i=0;i<this.restaurantListNode.children.length;i++){
            this.restaurantListNode.children[i].getComponent('ResSetup').lightOff()
        }
    },

    onLoad () {
        this.UserData = UserData
        require('request').offlinePromptNode = this.offlinePromptNode
        // open Accelerometer
        try{
            wx.startAccelerometer({interval:"normal"})
            wx.onAccelerometerChange((acc)=>{
                let len = Math.sqrt(2)
                UserData.runtimeState.gravity = cc.v2(acc.x/len *100 ,acc.y/len *100)
                if(Math.sqrt(acc.x * acc.x + acc.y * acc.y)>8){
                    this.earthShakeButton.getComponent('EarthShakeSkill').shake()
                }
            })
        }catch(err){
            //cc.log(err)
        }
        //open Gyroscope
        try{
            wx.startGyroscope()
            wx.onGyroscopeChange((GyroscopeDiffer)=>{//关闭陀螺仪感应
                //UserData.runtimeState.GyroscopeChangeCallBack(GyroscopeDiffer)
            })
        }catch(err){
            //cc.log(err)
        }
        try{
            wx.HideOn(()=>{
                UserData.runtimeState.HideScreen = true
            })
        }catch(err){
            //cc.log(err)
        }
        try{
            wx.ShowOn(()=>{
                UserData.runtimeState.HideScreen = false
            })
        }catch(err){
            //cc.log(err)
        }
    },
 
    update (dt) {
        this.viewSize=cc.view.getVisibleSize()
        COMMON_VALUE.resolution = cc.v2(this.viewSize.width,this.viewSize.height)
    },
});
